
const Koa = require('koa')
const app = new Koa()
import router from './router/index';
import cors from '@koa/cors';
import bodyParser from 'koa-bodyparser';
import { authMiddleware } from './middleware/auth';
import db from './dataAccess/db'; // 引入 MongoDB 连接代码
db.once('open', () => {
  console.log('数据库链接成功');
  // 使用中间件
  app.use(bodyParser());
  app.use(authMiddleware);
  app.use(cors());
  app
  .use(router.routes())
  .use(router.allowedMethods());
  app.listen(3001)
  console.log('[demo] start-quick is starting at port 3001')
  console.log("Hello, TypeScript!");
});
// 处理 MongoDB 连接错误
db.on('error', (error) => {
  console.error('MongoDB 链接错误:', error);
});

/**
 * 
 * 跨域允许部分网站的时候的配置
 * const corsOptions = {
    origin: 'http://localhost:5173', // 允许的前端地址
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization'],
  };
  app.use(cors(corsOptions));
 * 
*/

